/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import static com.lineage.server.model.skill.L1SkillId.ENTANGLE;
import static com.lineage.server.model.skill.L1SkillId.HASTE;
import static com.lineage.server.model.skill.L1SkillId.MASS_SLOW;
import static com.lineage.server.model.skill.L1SkillId.SLOW;

import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_SkillHaste;

/**
 * Class <code>Haste</code> 法师魔法:加速术 (40/0).
 * 增加目标50%的速度，效果不累计
 * 
 * @author jrwz
 * @version 2012-7-26上午05:34:45
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class Haste extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new Haste();
    }

    /** 法师魔法:加速术 (40/0). */
    private Haste() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (cha.getMoveSpeed() != 2) { // 缓速中以外
            if (cha instanceof L1PcInstance) {
                final L1PcInstance pc = (L1PcInstance) cha;
                if (pc.getHasteItemEquipped() > 0) {
                    return;
                }
                pc.setDrink(false);
                pc.sendPackets(new S_SkillHaste(pc.getId(), 1,
                        getBuffIconDuration));
            }
            cha.broadcastPacket(new S_SkillHaste(cha.getId(), 1, 0));
            cha.setMoveSpeed(1);
        } else { // 缓速中
            int skillNum = 0;
            if (cha.hasSkillEffect(SLOW)) {
                skillNum = SLOW;
            } else if (cha.hasSkillEffect(MASS_SLOW)) {
                skillNum = MASS_SLOW;
            } else if (cha.hasSkillEffect(ENTANGLE)) {
                skillNum = ENTANGLE;
            }
            if (skillNum != 0) {
                cha.removeSkillEffect(skillNum);
                cha.removeSkillEffect(HASTE);
                cha.setMoveSpeed(0);
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        cha.setMoveSpeed(0);
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            pc.sendPackets(new S_SkillHaste(pc.getId(), 0, 0));
            pc.broadcastPacket(new S_SkillHaste(pc.getId(), 0, 0));
        }
    }
}
